module.exports = function($scope, $modalInstance, $resource, siteInfo, toaster){

    $scope.siteInfo = { 'status': '1' };
    if(siteInfo){
        $scope.siteInfo.title = siteInfo.title;
        $scope.siteInfo.contacts_name = siteInfo.contacts_name;
        $scope.siteInfo.mobile = siteInfo.mobile;
        $scope.siteInfo.longitude = siteInfo.longitude;
        $scope.siteInfo.latitude = siteInfo.latitude;
        $scope.siteInfo.open_time = siteInfo.open_time;
        $scope.siteInfo.status = siteInfo.status;
        var a_list = siteInfo.address.split('-');
        $scope.siteInfo.province = {'name': a_list[0]}
        $scope.siteInfo.city = {'name': a_list[1]}
        $scope.siteInfo.address = a_list[2];
    } else {
        $scope.siteInfo.status = '1';
    }
    console.log($scope.siteInfo, '$scope.siteInfo')

    $scope.getCityList = function (code, list){
        $resource('/api/us/sc/city/arealist', {}, {}).save({'code': code.code}, function(res){
			if (res.errcode === 0) {
                $scope[list] = res.data;
			} else {
				toaster.error({title: '', body: res.errmsg});
			}
		});
    }
    $scope.getCityList('', 'provincelist')

    //确定
    $scope.ok = function () {
        if(!$scope.siteInfo.title){
            toaster.warning({title: '', body: '未填写站点名称'});
            return;
        }
        if(!$scope.siteInfo.contacts_name){
            toaster.warning({title: '', body: '未填写负责人'});
            return;
        }
        if(!$scope.siteInfo.mobile){
            toaster.warning({title: '', body: '未填写联系方式'});
            return;
        }
        if(!$scope.siteInfo.address){
            toaster.warning({title: '', body: '未填写详细地址'});
            return;
        }
        console.log($scope.siteInfo, '$scope.siteInfo')
        var para = {
            'title': $scope.siteInfo.title,
            'longitude': $scope.siteInfo.longitude,
            'latitude': $scope.siteInfo.latitude,
            'status': $scope.siteInfo.status,
            'contacts_name': $scope.siteInfo.contacts_name,
            'open_time': $scope.siteInfo.open_time,
            'mobile': $scope.siteInfo.mobile
        }
        if($scope.siteInfo.province){
            para.address = $scope.siteInfo.province.name + '-'
        } else {
            toaster.warning({title: '', body: '未选择站点信息'});
            return;
        }
        if($scope.siteInfo.city){
            para.address += $scope.siteInfo.city.name + '-'
        } else {
            toaster.warning({title: '', body: '未选择站点信息'});
            return;
        }
        para.address += $scope.siteInfo.address
        if(siteInfo && siteInfo.id){
            para.id = siteInfo.id
        }
        $resource('/api/as/tc/entitysite/saveEntitySite', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({title: '', body: '添加成功'});
				$modalInstance.close();
			} else {
				toaster.error({title: '', body: res.errmsg});
			}
		});
    };

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };


};